import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router";
import liuRouter from "./liuRouter";
import xiaoRouter from "./xiaoRouter";
import tanRouter from "./tanRouter";
import suRouter from "./suRouter";
// 引入登录状态机获取用户token
import {useUserLogin} from "../store/userLogin";

export const routes: RouteRecordRaw[] = [
    {
        path: '/', component: () => import('../layout/index.vue'), children: [
            ...tanRouter, ...suRouter, ...xiaoRouter, ...liuRouter
        ]
    },
    {path: '/login', component: () => import('../views/Login/Login.vue'), meta: {label: '登录', isShow: false}},
    {path: '/404', component: () => import('../views/notFound/NotFount.vue'), meta: {label: '404', isShow: false}},

]
export const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

let initWhiteList = ['/Login']
let blackList = {
    'zhiJ': ['/PressureStatistics/PressureList', '/RealTimeDta/DataList', '/DeviceEvent'],
    'diaoD': ['/PressureStatistics/PressureList', '/RealTimeDta/DataList', '/DeviceEvent', '/eventList'],
}
router.beforeEach((to, from, next) => {
    const useStore = useUserLogin()
    if (initWhiteList.includes(to.path)) {
        next()
    }else {
       if (useStore.userLoginObj) {
           if (useStore.userLoginObj["role"] === '检测员') {
               console.log(useStore.userLoginObj["role"])
               if (blackList.zhiJ.includes(to.path)) {
                   next('/404')
               }else {
                   next()
               }
           }else {
               next()
           }
           if (useStore.userLoginObj["role"] === '调度员') {
               if (blackList.diaoD.indexOf(to.path) !== -1) {
                   router.push('/404')
               }else {
                   next()
               }
           }else {
               next()
           }
       }else {
           next('/Login')
       }
    }
})
